package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constrant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 权限管理
 */
@RequestMapping("/permission")
@RestController
public class PermissionController {
@Reference
    private PermissionService permissionService;
    //新增权限
    @RequestMapping("/add")
    //前台提交formData,后台已经定义用CheckItem接受,封装
     /*
     @RequestBody 作用:解析提交过来的json数据,
     封装成指定的CheckItem对象
      */
     public Result add(@RequestBody Permission permission){


        try {
            permissionService.add(permission);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false,"新增权限失败,服务器正忙");
        }
        return new Result(true,"新增权限成功");
    }


    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult =permissionService.pageQuery(queryPageBean);
        return pageResult;

    }

    /**
     * 权限删除
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id){

        try {
            permissionService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            //失败
            return new Result(false,"删除失败,服务器正忙");
        }
        return new Result(true,"删除成功");
    }

    /**
     * 根据id查询权限信息
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
           Permission permission = permissionService.findById(id);
            return  new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,permission);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL,"服务器正忙");
        }
    }

    /**
     * 修改权限信息
     * @param permission
     * @return
     */
    @RequestMapping("/edit")
    public Result edit( @RequestBody Permission permission){
        try{
           permissionService.edit(permission);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, "修改失败","服务器正忙");
        }
        return  new Result(true,"修改成功");
    }

    /**
     * 查询所有的权限
     * @return
     */

    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List<Permission> list = permissionService.findAll();
            return  new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL,"服务器正忙");
        }

    }

}
